using System;
using System.Collections.Generic;
using SCore.Util;
using UnityEngine.SceneManagement;

namespace Function.SAsset
{
    // 场景管理器: 复杂游戏内的场景切换，Boot场景不受管理
    // 要求场景名和场景类同名，比如AaaScene.cs和AaaScene.unity，场景类继承自BaseScene
    public partial class XSceneManager : Singleton<XSceneManager>
    {
        private SceneTask _mainScene = null;

        /// <summary>
        /// 当前顶层场景的 SceneTask
        /// </summary>
        public SceneTask MainScene => _mainScene;

        /// <summary>
        /// 上一个场景Name
        /// </summary>
        private string _lastSceneName;

        // 要求场景名和场景类同名
        // 逻辑主线使用单场景模式
        // 如果有多场景，使用额外的stack管理，约束多场景的使用主要用于展示
        // loading ui不应该在这里处理，在业务层处理
        public SceneTask LoadSceneAsync<T>(Action<SceneTask> doneCallback = null) where T : BaseScene
        {
            return AddTask<T>(doneCallback);
        }

        public SceneTask LoadSceneAsyncByName(string sceneName, Action<SceneTask> doneCallback = null)
        {
            return AddTask(sceneName, doneCallback);
        }

        public SceneTask LoadAdditiveSceneAsyncByName(string sceneName, Action<SceneTask> doneCallback = null)
        {
            return AddTask(sceneName, doneCallback, LoadSceneMode.Additive);
        }


        public bool TryGet(string sceneName, out SceneTask sceneTask)
        {
            sceneTask = FindSceneTaskByName(sceneName);
            return sceneTask != null;
        }

        public Stack<SceneTask> GetSceneMap()
        {
            return _sceneStack;
        }

        public string GetLastSceneName()
        {
            if (_sceneStack.Count > 1)
            {
                return GetSecondFirstStack()._sceneName;
            }

            return _lastSceneName;
        }
    }
}